<?
class ManageBoothMenuEditForm extends Form
{
    var $hotel = array(), $room;
	
	function ManageBoothMenuEditForm()
	{
		Form::Form('ManageRoomForm');

		$this->link_css('style/ManageRoom/roomform.css');
		$this->link_css("style/paging.css");
		$this->link_css("style/hotel.css");
		$this->link_css('style/jquery-ui-1.9.2.custom.css');

		$this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
		//

        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
		
		$id = Url::getParamInt("id");
		
		if($id)
		{
			$this->room = Db_BoothMenu::getById($id);
		}
		
		if(!$this->room || $this->room['status'] == -1)
		{
			Url::redirect('manage_booth_menu',array('booth_id'));
		}
		
		$this->hotel = Db_Booth::getBoothById(Url::getParamInt("booth_id"));
	}

    function draw()
    {
        global $display;

        $this->beginForm(true);
        
      	$room = $this->room;
      	
         $display->add('room', $room);
          $display->add("WEB_DIR",WEB_DIR);

        if($this->hotel)
        {
            $display->add('hotelName', $this->hotel['name']);
        }
        else
        {
        	$display->add('hotelName', "<font color=red><i>(Đã bị xóa - không tồn tại)</i></font>");
        }
        $status_radios = array(1=>'Hiện', 2=>'Ẩn');

        if($room['status']<0)
        {
            $status_radios[-1] = 'Xóa';
        }

        $display->add('status_radios', 			$status_radios);


        $display->add("message",$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));
        
        $display->output('RoomForm');
        $this->endForm();
    }

    function on_submit()
    {
        global $display;
        $room['booth_id'] 		= $this->hotel['id'];
        $room['name'] 			= Url::get('name');

        mb_internal_encoding("UTF-8");
        $room['name'] = mb_strtoupper(mb_substr($room['name'],0,1)) . mb_substr($room['name'],1);

        $room['price'] 			= Url::cdouble(Url::get('price'));

        $room['status'] 		= Url::getInt('status');
        $room['desc'] 		= Url::get('desc');

        if (isset($_FILES["img_small"]) && $_FILES["img_small"]["size"] > 0) {
            $store_in = 'images/upload/boothmenu/';
            $name_small = md5($_FILES["img_small"]["tmp_name"]);
            move_uploaded_file($_FILES["img_small"]["tmp_name"], $store_in . $name_small.'.jpg');
            $room['img_small'] =  $store_in .$name_small.'.jpg';
        } else {
            unset($room['img_small']);
        }

        if (isset($_FILES["img_lager"]) && $_FILES["img_lager"]["size"] > 0) {
            $store_in = 'images/upload/boothmenu/';
            $name_lager = md5($_FILES["img_lager"]["name"]);
            move_uploaded_file($_FILES["img_lager"]["tmp_name"], $store_in . $name_lager.'.jpg');
            $room['img_lager'] =  $store_in . $name_lager.'.jpg';
        } else {
            unset($room['img_lager']);
        }

        //$room = HTML::cleanHtml($room);

        $valid = true;
        if($room['name']==''){
            $valid = false;
            $message = "Tên thuc don không được để trống!";
        }
        if($room['desc']==''){
            $valid = false;
            $message = "Mo ta thuc don không được để trống!";
        }
        if($room['price']=='' || $room['price'] ==0){
            $valid = false;
            $message = "gia không được để trống hoac bang 0!";
        }

        if($valid){
            if(Db_BoothMenu::update($room,$this->room['id']))
            {
                $this->setFormSucces("success","Save hành công!");
                //Url::redirect_current(array('cmd','hotel_id','success'=>1));
                $href = Url::get('href');
                if($href)
                {
                    Url::redirect_url($href);
                }
                else
                {
                    Url::redirect_current(array('booth_id'));
                }
            }
            else
            {
                $this->setFormError('message', 'Không lưu được phòng.');
            }
        }else{
            $this->setFormError('message', $message);
        }
    }
}